<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\CustomServer;
class Servicer extends Common{
    //客服模型
    protected $service;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->service=new CustomServer();
    }
    /**
     *客服列表
     *return json
     */
    public function servicerList(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $pageNo = $this->request->post('pageNo',1);
        $pageSize = $this->request->post('pageSize',10);
        $list=$this->service->page($pageNo,$pageSize)->order('id','desc')->select();
        $total =$this->service->count();
        if(empty($list)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['list']=$list;
            $msg['total']=$total;
        }
        return $msg;
    }
    /**
     *添加或修改客服
     * return json
     */
    public function addOrEditeServicer(){
          if(empty($this->adminInfo)){
              $msg['message']=checkMsg(LOGIN_ERROR);
              return $msg;
          }
          $input=$this->request->post();
            $check_error=$this->validate($input,'Servicer');
            if($check_error!==true){
                $msg=checkMsg($check_error);
                return ['message'=>$msg];
            }
          if(isset($input['id']) && !empty($input['id'])){ //修改
              $data=[
                  'nickname' =>$input['nickname'],
                  'wechat_code'  =>$input['wechat'],
                  'phone'   =>$input['phone'],
                  'qq_code'      =>$input['qq'],
                  'qr_code'  =>$input['qrcode'],
                  'update_time'  =>time()
              ];
             $res = $this->service->save($data,['id'=>$input['id']]);
          }else{  //添加
              $data=[
                  'nickname' =>$input['nickname'],
                  'wechat_code'  =>$input['wechat'],
                  'phone'   =>$input['phone'],
                  'qq_code'      =>$input['qq'],
                  'qr_code'  =>$input['qrcode'],
                  'add_time'=>time()
              ];
              $count = $this->service->where(['phone'=>$input['phone']])->whereOr(['wechat_code'=>$input['wechat']])->whereOr(['qq_code'=>$input['qq']])->count();
              if($count>0){
                  $msg['message']=checkMsg(SERVICER_EXITS);
                  return $msg;
              }
              $res = $this->service->insertGetId($data);
          }
          if($res){
              $msg['message']=checkMsg(OPERA_SUCCESS);
          }else{
              $msg['message']=checkMsg(OPERA_FAIL);
          }
          return $msg;
    }
    /**
     *获取客服信息
     * return json
     */
    public function servicerInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $id = $this->request->post('id');
        if(empty($id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $info = $this->service->where(['id'=>$id])->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info']=$info;
        }
        return $msg;
    }
    /**
     *删除客服
     * return json
     */
    public function deleteServicer(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $id = $this->request->post('id');
        if(empty($id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->service->where(['id'=>$id])->count();
        if($count<1){
            $msg['message']=checkMsg(SERVICER_NOT_EXITS);
            return $msg;
        }
        $res = $this->service->where(['id'=>$id])->delete();
        if($res){
            $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(DELETE_DATA_FAIL);
        }
        return $msg;
    }
}